package cn.xiaopengstack.persistent.repository;

import cn.xiaopengstack.eum.McpTransportTypeEnum;
import cn.xiaopengstack.mcp.model.McpConfigEntity;
import cn.xiaopengstack.mcp.repository.IMcpConfigRepository;
import cn.xiaopengstack.persistent.dao.IMcpConfigDao;
import cn.xiaopengstack.persistent.po.McpConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * @author jiangyangang
 */
@Repository
@Slf4j
public class McpConfigRepository implements IMcpConfigRepository {

    @Resource
    private IMcpConfigDao mcpConfigDao;

    @Override
    public McpConfigEntity queryMcpConfig(String mcpConfigKey) {
        McpConfig mcpConfig = mcpConfigDao.selectByKey(mcpConfigKey);
        assert mcpConfig != null;

        return McpConfigEntity.builder()
                .mcpAppKey(mcpConfig.getMcpAppKey())
                .mcpAppName(mcpConfig.getMcpAppName())
                .mcpConfigValue(JSONObject.parseObject(mcpConfig.getMcpConfigValue()))
                .mcpConfigJson(JSONObject.parseObject(mcpConfig.getMcpConfigJson()))
                .mcpTransportTypeEnum(McpTransportTypeEnum.valueOf(mcpConfig.getMcpTransportType()))
                .updateTime(mcpConfig.getUpdateTime())
                .createTime(mcpConfig.getCreateTime())
                .build();
    }
}
